package com.client.gui;

import java.awt.EventQueue;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;

import java.awt.Component;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.Window;

import javax.swing.JButton;
import javax.swing.SwingUtilities;

import org.apache.activemq.ActiveMQConnectionFactory;

import com.m.notowania.NotowaniaData;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class Notowania extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String user = null;
					Notowania frame = new Notowania(user);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	 
	
	public Notowania(final String user) {
		setTitle("mClient - Notowania");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 535, 337);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(30, 47, 369, 187);
		contentPane.add(scrollPane_1);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Data", "Index", "Warto\u015B\u0107", "+/-"
			}
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			boolean[] columnEditables = new boolean[] {
				false, true, true, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(95);
		table.getColumnModel().getColumn(1).setPreferredWidth(86);
		table.getColumnModel().getColumn(2).setPreferredWidth(87);
		table.getColumnModel().getColumn(3).setPreferredWidth(60);
		scrollPane_1.setViewportView(table);
		
		JLabel label = new JLabel("mClient");
		label.setForeground(new Color(0, 128, 128));
		label.setFont(new Font("Tahoma", Font.BOLD, 26));
		label.setBounds(409, 11, 110, 23);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("Notowania");
		label_1.setForeground(SystemColor.textHighlight);
		label_1.setFont(new Font("Tahoma", Font.BOLD, 15));
		label_1.setBounds(30, 17, 147, 19);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("<userName>");
		label_2.setBounds(151, 294, 88, 14);
		contentPane.add(label_2);
		label_2.setText(user);
		
		JLabel lblNewLabel = new JLabel("Zalogowany jako: ");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel.setBounds(61, 294, 88, 14);
		contentPane.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("Odwie\u017C");
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
		       String queueName = "notowania.queue";          
				
				 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
			        try{
			            Connection connection = connectionFactory.createConnection();
			            connection.start();
			             
			            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			            Destination queue = session.createQueue(queueName);
			            MessageConsumer consumer = session.createConsumer(queue);
			    
			            System.out.println("Consumer waiting...");

			      //Message message = consumer.receive();
			      ObjectMessage message = (ObjectMessage)consumer.receive();    
			            if(message instanceof ObjectMessage){
			        
			                NotowaniaData notow = (NotowaniaData)message.getObject();
			                System.out.println("Received"+notow.getData());  
			                String Data = notow.getData();
			                String Index = notow.getIndex();
			                String Wartosc = notow.getWartosc();
			                String WartoscStara = notow.getWartoscStara();
			                String Kurs = null;
			                
			                if (WartoscStara == null)
			                	WartoscStara = "0";
			                else
			                {			                			                
			                Double dWartosc = Double.parseDouble(Wartosc);
			                Double dWartoscStara = Double.parseDouble(WartoscStara);
			                
			                if(Wartosc.equals(WartoscStara))
			                	Kurs = "0";
			                else if(dWartosc < dWartoscStara)
			                	Kurs = "-";
			                else if(dWartosc > dWartoscStara)
			                	Kurs = "+";
			                }
			                DefaultTableModel model=(DefaultTableModel) table.getModel();
			                model.addRow(new Object []{				
			    					Data,Index,Wartosc,Kurs
			    	         });  
			                System.out.println("Received"+notow.getIndex());  
			                System.out.println("Received"+notow.getWartosc());  
			            }else{
			                System.out.println("Received message of unknown type");
			            }

			            consumer.close();
			            session.close();
			            connection.close();
			         
			        }catch(JMSException e){         
			            System.out.println("Problem creating connection to ActiveMQ");
			            e.printStackTrace();
			        }catch(Exception e){
			            e.printStackTrace();
			        }
				
			}
		});
		btnNewButton.setBounds(409, 70, 89, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Wyczysc");
		btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
				tableModel.setRowCount(0);
			}
		});
		btnNewButton_1.setBounds(409, 104, 89, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("Auto");
		btnNewButton_2.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_2.setBounds(409, 138, 89, 23);
		contentPane.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("Archiwum");
		btnNewButton_3.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Archiwum arch = new Archiwum(user);
				arch.setVisible(true);
			}
		});
		btnNewButton_3.setBounds(87, 245, 102, 23);
		contentPane.add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("Zamknij");
		btnNewButton_4.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnNewButton_4.setBounds(409, 279, 110, 23);
		contentPane.add(btnNewButton_4);
		
		JButton btnNewButton_5 = new JButton("Uprawnienia");
		btnNewButton_5.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//String user2 = label_2.getText();
				Uprawnienia upraw = new Uprawnienia(user);
				upraw.setVisible(true);
			}
		});
		btnNewButton_5.setBounds(199, 245, 102, 23);
		contentPane.add(btnNewButton_5);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\marmicc\\Desktop\\PRO\\Broker\\ico\\comp.png"));
		lblNewLabel_1.setBounds(0, 234, 64, 74);
		contentPane.add(lblNewLabel_1);
		
		JButton btnNewButton_6 = new JButton("Zamknij okno");
		btnNewButton_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
	            Window frame = SwingUtilities.windowForComponent((Component) e
	                    .getSource());
	            frame.setVisible(false);
			}
		});
		btnNewButton_6.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_6.setBounds(409, 245, 110, 23);
		contentPane.add(btnNewButton_6);
	}
}
